library(foreign)
library(survival)
library(mgcv)
library(stargazer)
#Divide data using 2006 as the threshold
ss.dat.f06 <- subset(ss.dat.f, year<=2005)
ss.dat.f08 <- subset(ss.dat.f, year>2005)

##Run regular GLM
#No maize and interaction
glm.mod.f06s <- glm(civconf ~ nlights_calib_mean+
                      logpop+lagcivconflagtemp+polity2+
                      logttime+temp+spi6+drug_y+splcivconf+nwstate
                    ,data=ss.dat.f06, family=binomial(link = "logit"))
#summary(glm.mod.f06)
#Predict on out of sample data
pred.glm08s <- predict(glm.mod.f06s, newdata=ss.dat.f08, se.fit=TRUE)


##With maize and interaction
glm.mod.f06 <- glm(civconf ~ maize_area+nlights_calib_mean+light_main+
                     logpop+lagcivconflagtemp+polity2+
                     logttime+temp+spi6+drug_y+splcivconf+nwstate
                   ,data=ss.dat.f06, family=binomial(link = "logit"))
#summary(glm.mod.f06)
#Predict on out of sample data
pred.glm08 <- predict(glm.mod.f06, newdata=ss.dat.f08, se.fit=TRUE)


###Run GAMs
#Without maize and interaction
gam.mod.f206s <- bam(civconf ~ s(nlights_calib_mean, bs='tp', sp=0.2) + 
                       s(pop, bs='tp', sp=0.2)+ 
                       lagcivconflagtemp+s(ttime, bs='tp', sp=0.2) +
                       polity2 + temp + spi6 + drug_y + splcivconf + nwstate
                     ,data=ss.dat.f06, family=binomial(link = "logit"))
#summary(gam.mod.f206s)
#Predict on out of sample data
pred.gam208s <- predict(gam.mod.f206s, newdata=ss.dat.f08, se.fit=TRUE)

###With maize and interaction
gam.mod.f206 <- bam(civconf ~ s(maize_area, bs='tp', sp=0.2) + 
                      s(nlights_calib_mean, bs='tp', sp=0.2) + 
                      ti(nlights_calib_mean, maize_area, bs=c('tp','tp'), sp=c(0.2,0.2)) +
                      s(pop, bs='tp', sp=0.2)+ 
                      lagcivconflagtemp+s(ttime, bs='tp', sp=0.2) +
                      polity2 + temp + spi6 + drug_y + splcivconf + nwstate
                    ,data=ss.dat.f06, family=binomial(link = "logit"))
#summary(gam.mod.f206)
#Predict on out of sample data
pred.gam208 <- predict(gam.mod.f206, newdata=ss.dat.f08, se.fit=TRUE)
